package org.mspring.mlog.interceptor;

import org.mspring.mlog.entity.UserEntity;
import org.mspring.mlog.security.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Gao Youbo
 * @since 2014年10月13日
 */
@Component
public class SettingInterceptor extends HandlerInterceptorAdapter {
    private SecurityService securityService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setAttribute("base", request.getContextPath());
        UserEntity user = securityService.getCurrentUser();
        if (user == null) {
            System.out.println(handler);
        }
        request.setAttribute("currentUser", user);
        return super.preHandle(request, response, handler);
    }

    @Autowired
    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }
}